<?php
global $_W,$_GPC;
load()->func('file');
load()->func('tpl');
$uniacid = $_W['uniacid'];
$sexarr = array('未知', '男', '女');
$job1 = explode(',', $this->module['config']['job1']);
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$opp = 'cards';
if ('post' == $op) {
  $id = intval($_GPC['id']);
  if (!empty($id)) {
    $item = pdo_fetch('SELECT * FROM ' . tablename('bbyscm_card') . (' where id=' . $id));
    $item["imgs"] = iunserializer($item["imgs"]);
    empty($item) ? message('亲,数据不存在！', '', 'error') : '';
  }
  if (checksubmit('submit')) {
    empty($_GPC['cardname']) ? message('姓名必填') : $cardname = $_GPC['cardname'];
    empty($_GPC['mobile']) ? message('手机号必填') : $mobile = $_GPC['mobile'];
    $job = empty($_GPC['job1']) ? $_GPC['job'] : $_GPC['job1'];
    $images = iserializer($_GPC['imgs']);
    $data = array(
      'uniacid' => $_W['uniacid'],
      'avater' => $_GPC['avater'], 
      'ai_card' => $_GPC['mail_id'], 
      'imgs'=> $images,
      'weixin' => $_GPC['weixin'], 
      'username' => $_GPC['username'], 
      'cardname' => $_GPC['cardname'], 
      'sex' => $_GPC['sex'],
      'mobile' => $_GPC['mobile'], 
      'mobile2' => $_GPC['mobile2'],  
      'company' => $_GPC['company'],  
      'job' => $job, 
      'email' => $_GPC['email'], 
      'weibo' => $_GPC['weibo'],  
      'address' => $_GPC['address'], 
      'QR_code' => $_GPC['QR_code'], 
      'qq' => $_GPC['qq'], 
      'industry' => $_GPC['industry'],  
      'desc' => $_GPC['desc'],  
      'longitude' => $_GPC['g_map']['lng'], 
      'latitude' => $_GPC['g_map']['lat'], 
      'gongkai' => 1, 
      'popularity' => $_GPC['popularity'], 
      'p_share' => $_GPC['p_share'], 
      'money' => $_GPC['money'], 
      'bp_qr' => $_GPC['bp_qr'],
      'qrcode' => $_GPC['qrcode'],
      'createtime' => time(),
    );
    if (empty($id)) {
      pdo_insert('bbyscm_card', $data);
      message('数据加成功！', $this->createWebUrl('cards', array('op' => 'display')), 'success');
      return NULL;
    }

    unset($data['uniacid']);
    pdo_update('bbyscm_card', $data, array('id' => $id));
    message('数据更新成功！', $this->createWebUrl('cards', array('op' => 'display')), 'success');
    return NULL;
  }

  include $this->template('web/cards');
  return NULL;
}

if ('del' == $op) {
  if (isset($_GPC['delete'])) {
    $ids = implode(',', $_GPC['delete']);
    $sqls = 'delete from  ' . tablename('bbyscm_card') . '  where id in(' . $ids . ')';
    pdo_query($sqls);
    message('删除成功！', referer(), 'success');
  }

  $id = intval($_GPC['id']);
  $row = pdo_fetch('SELECT id FROM ' . tablename('bbyscm_card') . ' WHERE id = :id', array(':id' => $id));

  if (empty($row)) {
    message('抱歉，数据不存在或是已经被删除！', $this->createWebUrl('cards', array('op' => 'display')), 'error');
  }

  pdo_delete('bbyscm_card', array('id' => $id));
  message('删除成功！', referer(), 'success');
  return NULL;
}

if ('display' == $op) {
  $pindex = max(1, intval($_GPC['page']));
  $psize = 10;
  $condition = '';

  if (!empty($_GPC['keyword'])) {
    $condition .= ' AND (cardname LIKE \'%' . $_GPC['keyword'] . '%\' ' . ' OR  mobile LIKE \'%' . $_GPC['keyword'] . '%\') ';
  }

  if (!empty($_GPC['cid1'])) {
    $condition .= '   and  industry =' . $_GPC['cid1'];
  }

  $list = pdo_fetchall('SELECT *  FROM ' . tablename('bbyscm_card') . ' WHERE uniacid =' . $uniacid . $condition . '  ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize);
  $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('bbyscm_card') . ' WHERE uniacid =' . $uniacid . $condition);
  $pager = pagination($total, $pindex, $psize);


  if (!empty($list)) {
    foreach ($list as $key => $card) {
      $list[$key]['avater'] = tomedia($card['avater']);
      $imgs = iunserializer($card['imgs']);
      foreach ($imgs as $k => $imgid) {
        $imgs[$k] = tomedia($imgid);
      }
      $list[$key]['imgs'] = $imgs;
    }
  }
  ini_set("memory_limit","-1");
  include $this->template('web/cards');
  return NULL;
}

if ('shenhe' == $op) {
  $id = intval($_GPC['id']);
  $issend = intval($_GPC['akey']) == 1 ? 0 : 1;
  $data1 = array('akey' => $issend);
  pdo_update('bbyscm_card', $data1, array('id' => $id));

  if ($issend == 1) {
    echo json_encode(array('a' => 1));
    return NULL;
  }

  echo json_encode(array('a' => 0));
  return NULL;
}

if ('jb' == $op) {
  $id = intval($_GPC['id']);

  if (!empty($id)) {
    pdo_update('bbyscm_card', array('openid' => NULL), array('id' => $id));
    echo json_encode(array('status' => true));
    return NULL;
  }
  include $this->template('web/cards');

  echo json_encode(array('status' => false));
}


include $this->template('web/cards');
?>
